import Util
import Config
import pygame
import copy
import os
import os.path
import glob

targetingreticle = None
lockedreticle = None

def init():
   global targetingreticle
   global lockedreticle

   targetingreticle = Util.load_image(Config.targetingreticle)
   lockedreticle = Util.load_image(Config.lockedreticle)

pos_dict = {'topleft': (0.0,0.0),
            'top': (0.5, 0.0),
            'topright': (1.0, 0.0),
            'right': (1.0, 0.5),
            'bottomright': (1.0, 1.0),
            'bottom': (0.5, 1.0),
            'bottomleft': (0.0, 1.0),
            'left': (0.0, 0.5),
            'center': (0.5, 0.5)}

def blit_anchored(surface, image, pos, anchor='topleft'):
   width, height = image.get_rect().size
   move = pos_dict[anchor]
   pos_real = (int(pos[0] - move[0]*width), int(pos[1] - move[1]*height))
   surface.blit(image, pos_real)
   return pygame.Rect(pos_real, (width, height))

def print_screen(image):
   if 'screenshots' not in os.listdir(os.curdir):
      os.mkdir('screenshots')
   cur_shots = os.listdir('screenshots')
   i = 0
   filename = "Screen%s.jpg" % str(i).zfill(3)
   while filename in cur_shots:
      i += 1
      filename = "Screen%s.jpg" % str(i).zfill(3)
   pygame.image.save(image, os.path.join('screenshots', filename))

def scale(image, new_size=[None, None], preserve_aspect_ratio=False):
   new_size = copy.copy(new_size)
   width, height = image.get_rect().size
   if new_size[0] is None:
      new_size[0] = width
   if new_size[1] is None:
      new_size[1] = height
   if preserve_aspect_ratio:
      old_ratio = width * 1.0/height
      new_ratio = new_size[0] * 1.0/new_size[1]
      if new_ratio > old_ratio: #New dimensions are wider 
         new_size[0] = int(new_size[1] * old_ratio)
      elif new_ratio < old_ratio: #New dimensions are taller
         new_size[1] = int(new_size[0] / old_ratio)
   new_image = pygame.Surface(new_size)
   new_image.set_colorkey(image.get_colorkey())
   for i in range(0, new_size[0]):
      for j in range(0, new_size[1]):
         pt = (int(i * 1.0 * width / new_size[0]), int(j * 1.0 * height / new_size[1]))
         new_image.set_at((i,j), image.get_at(pt))
   return new_image

def wireframe(image, color=(255,255,255)):
   width, height = image.get_rect().size
   def get_neighbors(pt):
      minx = -1
      maxx = 1
      miny = -1
      maxy = 1
      if pt[0] <= 0:
         minx = -1 * pt[0]
      if pt[1] <= 0:
         miny = -1 * pt[1]
      if pt[0] >= width - 1:
         maxx = -1 * (pt[0] - width + 1)
      if pt[1] == height - 1:
         maxy = -1 * (pt[1] - height + 1)
      pts = list()
      for i in range(minx, maxx+1):
         for j in range(miny, maxx+1):
            if i != 0 or j != 0:
               pts.append((pt[0]+i, pt[1]+j))
      return pts
   def is_in(pt):
      return (pt[0] >= 0 and pt[0] < width and pt[1] >= 0 and pt[1] < height)
   new_image = pygame.Surface((width+2, height+2))
   new_image.set_colorkey((0,0,0))
   key = image.get_colorkey()
   for a in range(-1, width+1):
      for b in range(-1, height+1):
         neighbors = get_neighbors((a,b))
         for n in neighbors:
            if (is_in(n) and image.get_at(n) != key) and (not is_in((a,b)) or image.get_at((a,b)) == key):
               new_image.set_at((a+1,b+1), color)
               break
   new_image.set_colorkey((0,0,0))
   return new_image
